package com.jblv.frame.common.utils;

import com.jblv.frame.common.constants.BaseConstant;
import com.jblv.frame.common.dto.UserTokenDTO;
import com.jblv.frame.common.exception.FrameException;
import com.jblv.frame.common.utils.jwt.JwtUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author happyonion.yangtao
 * @version 1.0
 * @title UserTokenUitls
 * @projectName jblv-frame
 * @packageName com.jblv.common.utils
 * @description UserTokenUitls
 * @date Create at 2020/4/30 23:10
 * @since jdk1.8
 */
public class UserTokenUitls {

    /**
     * 获取登录token
     * 获取用户的token信息
     *
     * @return
     */
    public static String createAccessToken(String appId, String userId,  String mobile, String userName, String roleType, String userType, String avatar, String deptId, String deptName) {
        Long nowTime = System.currentTimeMillis();

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("appId", appId);
        params.put("userId", userId);
        params.put("mobile", mobile);
        params.put("userName", userName);
        params.put("roleType", roleType);
        params.put("userType", userType);
        params.put("avatar", avatar);
        params.put("deptId", deptId);
        params.put("deptName", deptName);
        params.put("updateTime", nowTime);
        params.put("expireTime", nowTime + JblvFrameConfigUtils.getInstance().getTokenExpireExpireTime());

        String accessToken = JwtUtils.enhance(params);

        return accessToken;

    }


    /**
     * 获取登录对象
     *
     * @param request
     * @return
     */
    public static UserTokenDTO getUserTokenDTO(HttpServletRequest request) throws FrameException {

        // 解析header内的参数
        String token = request.getHeader(BaseConstant.TOKEN);

        UserTokenDTO dto = getUserTokenDTO(token);

        return dto;
    }


    /**
     * 根据token获取登录对象
     * @param token
     * @return
     */
    public static UserTokenDTO getUserTokenDTO(String token) {

        try {

            Map<String, Object> params = JwtUtils.decode(token);
            UserTokenDTO dto = new UserTokenDTO();

            Object obj = params.get("appId");
            dto.setAppId(String.valueOf(obj));
            obj = params.get("userId");
            dto.setUserId(String.valueOf(obj));
            obj = params.get("mobile");
            dto.setMobile(String.valueOf(obj));
            obj = params.get("userName");
            dto.setUserName(String.valueOf(obj));
            obj = params.get("roleType");
            if (obj != null) {
                dto.setRoleType((String)obj);
            }
            obj = params.get("userType");
            dto.setUserType(String.valueOf(obj));
            obj = params.get("avatar");
            dto.setAvatar(String.valueOf(obj));
            obj = params.get("deptId");
            if (obj != null) {
                dto.setDeptId(String.valueOf(obj));
            }
            obj = params.get("deptName");
            if (obj != null) {
                dto.setDeptName(String.valueOf(obj));
            }
            obj = params.get("updateTime");
            dto.setUpdateTime(Long.parseLong(String.valueOf(obj)));

            obj = params.get("expireTime");
            dto.setExpireTime(Long.parseLong(String.valueOf(obj)));

            return dto;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }


}
